<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>发布</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://at.alicdn.com/t/font_1440001_47llmi4hsil.css" rel="stylesheet">
    <link rel="stylesheet" href="http://flatlogic.github.io/awesome-bootstrap-checkbox/bower_components/Font-Awesome/css/font-awesome.css">
    <link rel="stylesheet" href="css/build.css">
    <link href="https://cdn.bootcss.com/wangEditor/10.0.13/wangEditor.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/globle.css" />
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/user.css">
    <link rel="stylesheet" href="css/media.css">
</head>

<body>
    <div id="header"></div>
    <!-- 主体 -->
    <section class="info">
        <div class="container">
            <form class="pushc">
                <ul class="mine">
                    <li><span>内容类型<em class="red">*</em></span>
                        <select class="form-control" name="type" id="type1">
                        </select>
                        <select class="form-control" name="type" id="type2">
                        </select>
                    </li>
                    <li id="bgShow"><span>曝光平台<em class="red">*</em></span>
                        <input id="bgInput" bgid="" type="text" class="" placeholder="请输入平台关键字">
                        <ul id="bgShowUp" style="display: none;">
                            <li></li>
                        </ul>
                    </li>
                    <li><span>标题<em class="red">*</em></span><input type="text" name="title" class="form-control" placeholder="请输入标题，最多25个字"></li>
                    <li><span>摘要<em class="red">*</em></span><input type="text" name="summary" class="form-control" placeholder=""></li>
                    <!-- <li class="h60"><span>内容</span><textarea name="details" id="" cols="30" rows="10"></textarea></li> -->
                    <li><span>内容<em class="red">*</em></span>
                        <div id="editor">
                        </div>
                    </li>
                    <li>
                        <span>上传图片<em class="red">*</em></span>
                    </li>
                    <div style="">
                        <ul class="upload-ul clearfix">
                            <li class="upload-pick">
                                <div class="webuploader-container clearfix" id="goodsUpload"></div>
                            </li>
                            <span class="btn btn-primary" id="btn">上传图片</span>
                        </ul>
                    </div>
                    <li><span>联系方式<em class="red">*</em></span><input type="text" name="contact" class="form-control" placeholder=""></li>
                    <li><span>热门标签<em class="red">*</em></span>
                        <ul id="tags">
                        </ul>
                    </li>
                    <li><span>是否匿名发布</span>
                        <span class="radio radio-primary radio-single">
                            <input type="radio" name="isAnonymous" value="1">
                            <label>是</label>
                        </span>
                        <span class="radio radio-primary radio-single">
                            <input type="radio" name="isAnonymous" value="2" checked>
                            <label>否</label>
                        </span>
                        
                    </li>
                </ul>
                <div class="submit text-center">
                    <a href="javaScript:;" class="btn btn-default btn-lg center-block" id="btn-all">提交</a>
                </div>
            </form>
        </div>
    </section>
    <div id="footer"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"> </script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- <script src="https://cdn.bootcss.com/bootstrap-fileinput/5.0.6/js/fileinput.min.js"></script> -->
<script src="https://cdn.bootcss.com/wangEditor/10.0.13/wangEditor.min.js"></script>
<script src="js/webuploader.min.js"></script>
<script src="js/diyUpload.js"></script>
<script src="js/mine.js"></script>
<script src="js/core.js"></script>
<script>
$(function() {
    $("#header").load("./header.html")
    $("#footer").load("./footer.html")
    // 富文本编辑器
    var E = window.wangEditor
    var editor = new E('#editor')
    editor.create()
    // document.getElementById('btn1').addEventListener('click', function () {
    //     // 读取 html
    //     alert(editor.txt.html())
    // }, false)
    // 二级联动下拉
    Getdata(userType, {
        userType: "agnet_news_category" //userType：user_news_category（普通用户)，agnet_news_category（攥稿人），merchant_news_category（平台商）
    }).then(res => {
        console.log(res)
        let typedata1 = "";
        let typedata2 = "";
        // 一级赋值
        $.each(res.data, function(index, val) {
            typedata1 = `<option value="${index}">${val.lable}</option>`;
            $("#type1").append(typedata1);
        })
        // 二级默认赋值
        let firstV = $("#type1 option:first-child").val();
        $.each(res.data[firstV].nodes, function(index, val) {
            typedata2 = `<option value="${val.id}">${val.lable}</option>`;
            $("#type2").append(typedata2);
        })
        // 一级选择
        $("#type1").change(function() {
            var selValue = $(this).val();
            console.log(selValue)
            $("#type2 option").remove();
            $.each(res.data[selValue].nodes, function(index, val) {
                typedata2 = `<option value="${val.id}">${val.lable}</option>`;
                $("#type2").append(typedata2);
            })
        })
        $("#type2").change(function() {
            if ($(this).val() == 16) {
                $("#bgShow").css("display","block");
                $("#bgInput").keyup(function(event) {
                    let keyword = $("#bgInput").val();
                    if (keyword == '') {
                        $('#bgShowUp').css("display", "none");
                        return false;
                    }
                    $('#bgShowUp').css("display", "block");
                    Getdata(busList, { name: keyword, type: -1, size: 5000, current: 1 }).then(res => {
                        let bgList = "";
                        if (res.data.records.length == 0) {
                            bgList += `<li>没有匹配到任何平台!</li>`;
                            $('#bgShowUp').html(bgList);
                            return false;
                        }
                        $.each(res.data.records, function(index, val) {
                            if (index > 10) {
                                return false;
                            }
                            bgList += `<li bgid=${val.id}>${val.name}</li>`;
                        })
                        $('#bgShowUp').html(bgList);
                        $.each($('#bgShowUp li'), function(index, val) {
                            $(this).click(function() {
                                $("#bgInput").val($(this).html());
                                $("#bgInput").attr("bgid", $(this).attr("bgid"));
                                $('#bgShowUp').css("display", "none");
                            })
                        })

                        console.log(res.data.records[0].name)
                    })
                })
            }else{
                $("#bgShow").css("display","none");                
            }
        })
        // 查询曝光平台 bgShowUp
        // input值发生改变


    })




    //上传图片
    let file = "";
    var $tgaUpload = $('#goodsUpload').diyUpload({
        url: apiurl + 'exchange/attachment/fileUpload',
        success: function(res) {
            console.log(res)
            file = res.data;
            console.log(file)
        },
        error: function(err) {},
        buttonText: '',
        accept: {
            title: "Images",
            extensions: 'gif,jpg,jpeg,bmp,png'
        },
        thumb: {
            width: 120,
            height: 90,
            quality: 100,
            allowMagnify: true,
            crop: true,
            type: "image/jpeg"
        }
    });
    $("#btn").click(function() { $tgaUpload.upload() });
    // 获取热门标签tags
    Getdata(getValueByName, { name: "hotDynamicTags" }).then(res => {
        console.log(res)
        let tags = res.data.value.split('-');
        console.log(tags)
        let tagsItem = "";
        $.each(tags, function(index, val) {
            tagsItem = `<li class="checkbox checkbox-primary"><input type="checkbox" name="item" id="i${index}"  value="${val}"><label for="i${index}">${val}</label></li>`;
            $("#tags").append(tagsItem);
        })
    })
    // 提交
    console.log(userInfo)
    // $("#btn-all").addEventListener('click', function () {
    //     // 读取 html
    //     alert(editor.txt.html())
    // }, false)
    $("#btn-all").click(function() {
        // if()
        // var details = editor.txt.html();
        // console.log(content)
        console.log($("#type2").val())
        let categoryId = $("#type2").val(); //内容类型
        let type = null; //1平台 2商户 3用户
        if (userInfo.enterType) {
            type = 2;
        } else {
            type = 3;
        }
        let title = $("input[name='title']").val(); //标题
        let summary = $("input[name='summary']").val(); //摘要
        let details = editor.txt.html(); //内容
        let contact = $("input[name='contact']").val(); //联系方式
        let popularTags = ""; //热门标签
        $.each($("#tags input[type=checkbox]:checked"), function(index, val) {
            console.log($(this).val());
            popularTags += $(this).val() + "-";
        })
        let isAnonymous = $("input[name='isAnonymous']:checked").val();
        let merchantId = $("#bgInput").attr("bgid");
        if(merchantId == ''){
            merchantId = null;
        }
        $.ajax({
            url: apiurl + sendInformation,
            type: 'POST',
            dataTyp: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "categoryId": categoryId,
                "type": type,
                "title": title,
                "summary": summary,
                "details": details,
                "newsPath": file,
                "contact": contact,
                "popularTags": popularTags,
                "isAnonymous": isAnonymous,
                "merchantId": merchantId,
                "status": "1"
            }),
            success: res => {
                if (res.success == false) {
                    alert(res.msg)
                    window.location.href = "login.html";
                } else {
                    alert(res.msg)
                }
                console.log(res);
            },
        })
    })


})
</script>

</html>